package com.unicom.adapter;

import java.util.ArrayList;

import javax.crypto.spec.IvParameterSpec;

import com.myteam.mystory.quickaction.ActionItem;
import com.myteam.mystory.quickaction.QuickAction;
import com.unicom.model.StoryModel;
import com.unicom.mystory.R;

import android.content.Context;
import android.net.rtp.RtpStream;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.RatingBar;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.RelativeLayout.LayoutParams;

public class AllStoryAdapter extends BaseAdapter implements OnClickListener {
	private ArrayList<StoryModel> listStoryModel = new ArrayList<StoryModel>();
	private Context context;
	private LayoutInflater mInflater;

	// QuickAction
	private int mSelectedRow = 0;
	private ImageView mMoreIv = null;
	private static final int ID_ADD = 1;
	private static final int ID_ACCEPT = 2;
	private static final int ID_UPLOAD = 3;

	// =====================

	public AllStoryAdapter(ArrayList<StoryModel> listStoryModel, Context context) {
		super();
		this.listStoryModel = listStoryModel;
		this.context = context;
		this.mInflater = LayoutInflater.from(context);
	}

	@Override
	public int getCount() {
		return listStoryModel.size();
	}

	@Override
	public Object getItem(int position) {
		return listStoryModel.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {

		ActionItem likeCount = new ActionItem(
				"100 like",
				"  Adjhkfajjgasgfeasur iyauryaif dhasufryar ajfgru sayfeaaiusfd asjgufiarga",
				5);
		final QuickAction mQuickAction = new QuickAction(context);
		mQuickAction.addActionItem(likeCount);
		mQuickAction.setOnActionItemClickListener(new QuickAction.OnActionItemClickListener() {
					@Override
					public void onItemClick(QuickAction quickAction, int pos,int actionId) {
						ActionItem actionItem = quickAction.getActionItem(pos);
						if (actionId == ID_ADD) { // Add item selected
							Toast.makeText(context, "Add item selected on row " + mSelectedRow,Toast.LENGTH_SHORT).show();
						} else {
							Toast.makeText(context,actionItem.getTitle()+ " item selected on row "+ mSelectedRow, Toast.LENGTH_SHORT).show();
						}
					}
				});

		mQuickAction.setOnDismissListener(new PopupWindow.OnDismissListener() {
			@Override
			public void onDismiss() {
			}
		});

		final ViewHolder holder;
		if (convertView == null) {
			convertView = mInflater.inflate(R.layout.all_story_adapter, null);
			holder = new ViewHolder();
			holder.ivImg = (ImageView) convertView.findViewById(R.id.adapter_img_story);
			holder.tvName = (TextView) convertView.findViewById(R.id.adapter_name_story);
			holder.tvAuthor = (TextView) convertView.findViewById(R.id.adapter_author_story);
			holder.cbView = (CheckBox) convertView.findViewById(R.id.adapter_view_story);
			holder.rbRate = (RatingBar) convertView.findViewById(R.id.adapter_rate_story);
			holder.ivBook = (ImageView) convertView.findViewById(R.id.adapter_book_story);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		StoryModel storyModel = listStoryModel.get(position);
		holder.tvName.setText(storyModel.getNameStory());
		holder.tvAuthor.setText(storyModel.getAuthor());
		holder.cbView.setText(storyModel.getViewCount() + "");
		holder.rbRate.setRating(storyModel.getRate());
		holder.ivImg.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				mQuickAction.show(v);
			}
		});
		return convertView;
	}

	class ViewHolder {
		private ImageView ivImg;
		private TextView tvName;
		private TextView tvAuthor;
		private CheckBox cbView;
		private RatingBar rbRate;
		private ImageView ivBook;
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub

	}

}
